﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using FullDuplexTest.Contracts;

namespace FullDuplexTest.Silverlight
{
    public class ChatResponse : IChatResponse
    {
        private readonly MainPage _page;
        private bool _useAlternateColor;

        public ChatResponse(MainPage page)
        {
            _page = page;
            new Thread(WatchLastPing).Start();
        }

        private void WatchLastPing()
        {
            while(!Environment.HasShutdownStarted)
            {
                if(DateTime.Now - _lastPing > TimeSpan.FromSeconds(5))
                {
                    _page.Dispatcher.BeginInvoke(() =>
                                                     {
                                                         if (PingTimedOut == null)
                                                         {
                                                             return;
                                                         }

                                                         PingTimedOut();
                                                         _lastPing = DateTime.Now;
                                                     });
                }

                Thread.Sleep(5000);
            }
        }

        public void Notify(ChatMessage message)
        {
            _page.Dispatcher.BeginInvoke(() =>
                                             {
                                                 var bgColor = new SolidColorBrush(_useAlternateColor
                                                                                       ? Colors.LightGray
                                                                                       : Colors.White);
                                                 var userColor =
                                                     new SolidColorBrush(message.UserName == _page.Username.Text
                                                                             ? Colors.Red
                                                                             : Colors.Blue);
                                                 var item = new ListBoxItem {Background = bgColor};
                                                 var panel = new StackPanel{Orientation = Orientation.Horizontal};
                                                 var userName = new TextBlock {Text = message.UserName + ": ", Foreground = userColor, FontWeight = FontWeights.Bold};
                                                 var msg = new TextBlock
                                                                   {
                                                                       Text = message.Message,
                                                                       TextWrapping = TextWrapping.Wrap
                                                                   };
                                                 panel.Children.Add(userName);
                                                 panel.Children.Add(msg);
                                                 item.Content = panel;
                                                 _page.ChatHistory.Items.Add(item);
                                                 _useAlternateColor = !_useAlternateColor;
                                             });
        }

        public event Action PingTimedOut;

        private DateTime _lastPing = DateTime.Now;

        public void Ping()
        {
            _lastPing = DateTime.Now;
            _page.Dispatcher.BeginInvoke(() => _page.ChatHistory.Background = new SolidColorBrush(Colors.White));
        }
    }
}
